package com.example.ncov_management_system.controller;

import com.example.ncov_management_system.entity.Record;
import com.example.ncov_management_system.entity.Student;
import com.example.ncov_management_system.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@RestController
@Slf4j
@RequestMapping("/data")
public class DataController {
    @Autowired
    private StudentService studentService;

    @GetMapping("/person_record")
    public List<Map<String, String>> personRecord(HttpSession session) {
        Student student = studentService.findById(((Student) session.getAttribute("student")).getId());
        List<Record> records = student.getRecords();
        log.info("{}", records);
        ArrayList<Map<String, String>> list = new ArrayList<>();
        if (records.size() < 20) {
            for (Record record : records) {
                HashMap<String, String> map = new HashMap<>();
                LocalDateTime localDateTime = record.getCreateTime();
                map.put("date", LocalDate.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth()).toString());
                map.put("temperature", record.getTemperature() + "");
                list.add(map);
            }
        } else {
            List<Record> subList = records.subList(records.size() - 20, records.size());
            for (Record record : subList) {
                HashMap<String, String> map = new HashMap<>();
                LocalDateTime localDateTime = record.getCreateTime();
                map.put("date", LocalDate.of(localDateTime.getYear(), localDateTime.getMonth(), localDateTime.getDayOfMonth()).toString());
                map.put("temperature", record.getTemperature() + "");
                list.add(map);
            }
        }


        return list;
    }
}
